var fs = require('fs');
var path = require('path');

var log=require("../paca/plog");


//关于文件处理的简单封装


// 创建所有目录
var mkdirs = module.exports.mkdirs = function(dirpath, mode, callback) {
        fs.exists(dirpath, function(exists) {
        if(exists) {
                callback(dirpath);
        } else {
                //尝试创建父目录，然后再创建当前目录
                mkdirs(path.dirname(dirpath), mode, function(){
			fs.chmod(path.dirname(dirpath), 0777, function(err){ });
                        fs.mkdir(dirpath, mode, callback);
                });
        }
    });
};



var ReadFile = module.exports.ReadFile = function(_path, _filename, _cb)
{
	var p = path.join(_path, _filename);
	fs.readFile(p, 'utf8', function (err, data) {
     if (err) _cb(1, "");
     _cb(0, data);
    });
}



//_cb(_err);
var WriteFile = module.exports.WriteFile = function(_path, _filename, _content, _cb)
{
	var p = path.join(_path, _filename);
	mkdirs(_path, 0777, function()
	{
		fs.chmod(_path,0777, function(err){});
		fs.writeFile(p, _content,  function (err) {
 			if (err) _cb(1);
			fs.chmod(p,0777, function(err){});
			_cb(0) //文件被保存
		});;
	})
}
